package com.hound.map;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.amelie.driving.impl.DrivingDirectionsGoogleKML;
import com.google.android.maps.*;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.location.*;
//import android.app.Activity;
import android.os.Bundle;
import android.sax.EndElementListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HelloMapViewActivity extends MapActivity {

	double latitude;
	double longitude;
	LinearLayout linearLayout;
	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable drawable;
	HelloItemizedOverlay itemizedOverlay;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        setContentView(R.layout.main);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
        itemizedOverlay = new HelloItemizedOverlay(drawable);
        
        
        final TextView view = (TextView) findViewById(R.id.texto1);
        final EditText texto = (EditText) findViewById(R.id.editText1);
       
       // setContentView(view);
        
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v){
        	
        		 String teste = texto.getText().toString();
				 Geocoder geocoder = new Geocoder(getApplicationContext());
			        try {
						List<Address> addresslist = geocoder.getFromLocationName(teste, 1);
						Address endereco = addresslist.get(0);
						if ( endereco.hasLatitude() && endereco.hasLongitude()){
							latitude = endereco.getLatitude();
							longitude = endereco.getLongitude();
							int lat1e6 = (int)(latitude * 1e6);
							int lng1e6 = (int)(longitude * 1e6);
							view.setText(lat1e6 +" e "+lng1e6);
							
							GeoPoint point = new GeoPoint(lat1e6, lng1e6);
							//GeoPoint point2 = new GeoPoint(-27601207, -48522474);
							String start = lat1e6+","+lng1e6;
							String end = "-27601207,-48522474";
							OverlayItem overlayitem = new OverlayItem(point, "", "");
							itemizedOverlay.addOverlay(overlayitem);
							mapOverlays.add(itemizedOverlay); 
							MapController mapa = mapView.getController();
							mapa.animateTo(point);
							mapa.setZoom(15);
							GooglePoints listaDePontos = new GooglePoints();
							String urlString = listaDePontos.getUrl("BeiraMar%20Shopping", "Luiz%20Oscar%20de%20Carvalho");
							Log.i("mapas", urlString);
							String polyline = listaDePontos.getConnection(urlString);
							ArrayList<GeoPoint> geopoints = new ArrayList<GeoPoint>();
							geopoints = listaDePontos.decodePolyline(polyline);
							PathOverlay desenho = new PathOverlay(geopoints);
							List<Overlay> listOfOverlays = mapView.getOverlays();
					        listOfOverlays.clear();
					        listOfOverlays.add(desenho);
							
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        	}

			
        });
        
        
        
    }
  
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}